package com.qf.shop.controller;


import com.alibaba.fastjson.JSON;
import com.qf.shop.entity.Type;
import com.qf.shop.service.TypeService;
import com.qf.shop.service.impl.TypeServiceImpl;
import com.qf.shop.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/type")
public class TypeController extends BaseServlet {

    /**
     * 展示所有商品类型
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String findAllType(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //1.调用业务层，查询所有商品分类数据
        TypeService typeService = new TypeServiceImpl();
        List<Type> types = typeService.findAllType();
//        Gson gson = new Gson();
//        String json = gson.toJson(types);
//        return json;

        //2.将数据放入request 域中保存，用来携带到页面中
        request.setAttribute("goodsTypeList",types);

        //3。使用请求转发，跳转到商品分类展示页面
        return Constants.FORWARD + "/admin/showGoodsType.jsp";
    }
    /**
     * 商品添加下拉框需要的商品分类数据
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1. 调用service业务层, 查询所有商品分类数据
        TypeService typeService = new TypeServiceImpl();
        List<Type> list = typeService.findAllType();

        //2. 将商品分类集合对象数据, 转换成json格式字符串
        String jsonStr = JSON.toJSONString(list);

        //3. 通过流将json格式字符串写回到原页面中
        response.getWriter().println(jsonStr);
    }

    /**
     * 删除某一个商品类型的方法
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String deleteType(HttpServletRequest request,HttpServletResponse response) throws IOException, SQLException {
        //1.获取请求参数
        String tid = request.getParameter("tid");
        //2.调用业务层
        TypeService typeService = new TypeServiceImpl();
        int result = typeService.delete(tid);
        //3.响应
        if(result>0){
            return Constants.FORWARD + "/type?method=findAllType";
        }
        request.getSession().setAttribute("msg", "程序异常!请稍后再试！");
        response.sendRedirect("/message.jsp");
        return null;
    }

    /**
     * 修改商品的方法
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String updateType(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //1.获取请求参数
        Integer tid = Integer.valueOf(request.getParameter("tid"));
        String tname = request.getParameter("tname");
        String tinfo = request.getParameter("tinfo");

        //2.构建商品类，调用业务逻辑
        Type type = new Type(tid,tname,tinfo);
        TypeService typeService = new TypeServiceImpl();
        typeService.update(type);
        if(type != null){
            return Constants.FORWARD + "/type?method=findAllType";
        }
        request.getSession().setAttribute("msg", "程序异常!请稍后再试！");
        response.sendRedirect("/message.jsp");
        return null;
    }

    public String addType(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String tname = request.getParameter("tname");
        String tinfo = request.getParameter("tinfo");
        TypeService typeService = new TypeServiceImpl();
        int result = typeService.insert(tname,tinfo);


        request.getSession().setAttribute("msg", "程序异常!请稍后再试！");
        response.sendRedirect("/message.jsp");
        return null;
    }
}
